#include "guilcd_sdl.h"
#include "gui_sdl.h"
#include "core.h"
#include "stdafx.h"

enum {
	ID_LCD,
};

unsigned char redColors[MAX_SHADES+1];
unsigned char greenColors[MAX_SHADES+1];
unsigned char blueColors[MAX_SHADES+1];

void initLCD() {
#define LCD_HIGH	255
	for (int i = 0; i <= MAX_SHADES; i++) {
		redColors[i] = (0x9E*(256-(LCD_HIGH/MAX_SHADES)*i))/255;
		greenColors[i] = (0xAB*(256-(LCD_HIGH/MAX_SHADES)*i))/255;
		blueColors[i] = (0x88*(256-(LCD_HIGH/MAX_SHADES)*i))/255;
	}
	//SDL_LockSurface(lcdsurface);
	printf_d("[SDLWabbitemu] [initLCD] Function called!\n");
}

void LCDOnPaint()
{
	PaintLCD();
	LCD_t *lcd = lpCalc->cpu.pio.lcd;
	lpCalc->sb_refresh = clock();
	printf("FPS: %0.2lf", lcd->ufps);
}

void PaintLCD()
{
	unsigned char *thescreen;
	LCD_t *lcd = lpCalc->cpu.pio.lcd;
	int scale = lpCalc->scale;
	int draw_width = lcd->width * scale;
	int draw_height = 64 * scale;
	
	int x,y = 0;
	if (lcd->active == false) {
		unsigned char lcd_data[128*64];
		memset(lcd_data, 0, sizeof(lcd_data));
		unsigned char rgb_data[128*64*3];
		int i, j;
		for (i = j = 0; i < 128*64; i++, j+=3) {			
			// SUPER UGLY HACK:
			// We count up and determine rows, columns and draw the pixel for each
			
			if (x == 128) {
				x = 0;
				y++;
			}
			//DrawPixel(lcdsurface, redColors[lcd_data[i]], greenColors[lcd_data[i]], blueColors[lcd_data[i]], x, y);
			setPixel(x, y, RGB(redColors[lcd_data[i]], greenColors[lcd_data[i]], blueColors[lcd_data[i]]), buffer);
			x++;
		}
	} else {
		thescreen = LCD_image( lpCalc->cpu.pio.lcd ) ;
		unsigned char rgb_data[128*64*3];
		int i, j;
		for (i = j = 0; i < 128*64; i++, j+=3) {
			if (x == 128) {
				x = 0;
				y++;
			}
			//DrawPixel(lcdsurface, redColors[thescreen[i]], greenColors[thescreen[i]], blueColors[thescreen[i]], x, y);
			setPixel(x, y, RGB(redColors[thescreen[i]], greenColors[thescreen[i]], blueColors[thescreen[i]]), buffer);
			x++;
		}
	}
}

